﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RestFullChatAJAX.Models;
using Newtonsoft.Json.Linq;

namespace RestFullChatAJAX
{
    public class IndexManager
    {
        /*
         * Index of UserName in this list equal index of his "store message and his "list
         * friend submit"...
         */
        private static List<IndexStore> lstIndexMesStore = new List<IndexStore>();
        private static List<YahooInfo> lstIndexYahooStore = new List<YahooInfo>();
        public static object lockIndex = new object();
        public static object lockYahoo = new object();
        public static readonly String CONSUMERKEY = "TestAPI";
        public static readonly String CONSUMERKEYSECRECT = "TestAPI";

        public static int FindStoreIndex(String UserName)
        {
            lock (lockIndex)
            {
                for (int i = 0; i < lstIndexMesStore.Count; i++)
                    if (lstIndexMesStore[i].Username.Trim().CompareTo(UserName) == 0)
                        return i;
                return -1;
            }
        }

        public static void Add(String UserName)
        {
            lock (lockIndex)
            {
                lstIndexMesStore.Add(new IndexStore(UserName.Trim()));
            }
        }

        public static void Remove(String UserName)
        {
            lock (lockIndex)
            {
                for (int i = 0; i < lstIndexMesStore.Count; i++)
                    if (lstIndexMesStore[i].Username.Trim().CompareTo(UserName) == 0)
                    {
                        lstIndexMesStore.RemoveAt(i);
                    }
            }
        }

        public static int FindYahooIndex(String UserName)
        {
            lock (lockYahoo)
            {
                for (int i = 0; i < lstIndexYahooStore.Count; i++)
                    if (lstIndexYahooStore[i].Username.Trim().CompareTo(UserName) == 0)
                        return i;
                return -1;
            }
        }

        public static int SetYahooIndex(YahooInfo yi)
        {
            lock (lockYahoo)
            {
                for (int i = 0; i < lstIndexYahooStore.Count; i++)
                    if (lstIndexYahooStore[i].Username.Trim().CompareTo(yi.Username) == 0)
                    {
                        lstIndexYahooStore[i] = null;
                        lstIndexYahooStore[i] = yi;
                    }
                return -1;
            }
        }

        public static void AddYahoo(YahooInfo info)
        {
            lock (lockYahoo)
            {
                lstIndexYahooStore.Add(info);
            }
        }

        public static void RemoveYahoo(String UserName)
        {
            lock (lockYahoo)
            {
                for (int i = 0; i < lstIndexYahooStore.Count; i++)
                    if (lstIndexYahooStore[i].Username.Trim().CompareTo(UserName) == 0)
                    {
                        lstIndexYahooStore.RemoveAt(i);
                    }
            }
        }
        public static int GetCounter(int index)
        {
            int counter = 0;
            lock (lockIndex)
            {
                counter = lstIndexMesStore[index].Counter;
            }
            return counter;
        }

        public static void SetCounter(int index, int counter)
        {
            lock (lockIndex)
            {
                lstIndexMesStore[index].Counter = counter;
            }
        }

        public static String ListtoStringJSon(String jsonAlive, JArray accLst)
        {
            String str = "'Username':'{0}','State':'{1}',";
            String result = "[";            
            if (!String.IsNullOrEmpty(jsonAlive))
            {
                result = "";
                //Cut "]" end add ","                
                result = jsonAlive.Substring(0, jsonAlive.Length - 1).Trim() + ",";
            }
            foreach (JObject a in accLst)
            {
                result += '{' + String.Format(str, a["Username"].ToString(), a["State"].ToString()).Trim() + "},";
            }
            result = result.Substring(0, result.Length - 1).Trim(); //Cut "," end
            return result + "]";
        }

        internal static YahooInfo GetYahooInfo(int index)
        {
            lock (lockYahoo)
            {
                if (index != -1)
                    return lstIndexYahooStore[index];
            }
            return null;
        }
    }
}